<template>
    <div class="payment-card-page">
        <!-- 页面头部 -->
        <TopNav
                title="添加银行卡"
                :showBack="true"
                :showAdd="false"
        />
        <!-- 表单区域 -->
        <van-form
                label-align="top"
                @submit="handleSubmit"
                @failed="handleFormError"
                class="payment-form"
        >

            <!-- 持卡人姓名 -->
            <van-field
                    v-model="formData.username"
                    name="username"
                    :rules="[{ required: true, message: '请输入持卡人姓名' }]"
                    class="form-field"
                    placeholder="请输入持卡人姓名"
            >
                <template #label>
                    持卡人姓名<span class="required"> *</span>
                </template>
            </van-field>

            <!-- 身份证号 -->
            <van-field
                    v-model="formData.idcard"
                    name="idcard"
                    :rules="[{ required: true, message: '请输入身份证号' }, { pattern: idCardReg, message: '请输入正确的身份证号' }]"
                    class="form-field"
                    placeholder="请输入身份证号"
            >
                <template #label>
                    身份证号<span class="required"> *</span>
                </template>
            </van-field>

            <!-- 银行选择 -->
            <van-field
                    v-model="formData.bankName"
                    name="bankName"
                    placeholder="请选择开户银行"
                    :rules="[{ required: true, message: '请选择开户银行' }]"
                    readonly
                    is-link
                    clickable
                    @click="showBankPicker = true"
                    class="form-field"
            >
                <template #label>
                    开户银行<span class="required"> *</span>
                </template>
            </van-field>


            <!-- 银行卡号 -->
            <van-field
                    v-model="formData.cardNumber"
                    name="cardNumber"
                    placeholder="请输入银行卡号"
                    :rules="[{ required: true, message: '请输入银行卡号' }]"
                    class="form-field"
            >
                <template #label>
                    银行卡号<span class="required"> *</span>
                </template>
            </van-field>

            <!-- 预留手机号 -->
            <van-field
                    v-model="formData.phone"
                    name="phone"
                    placeholder="请输入银行预留手机号"
                    :rules="[{ required: true, message: '请输入银行预留手机号' }, { pattern: phoneReg, message: '请输入正确的手机号' }]"
                    class="form-field"
            >
                <template #label>
                    银行预留手机号<span class="required"> *</span>
                </template>
            </van-field>

            <!-- 验证码 -->
            <van-field
                    v-model="formData.vcode"
                    name="vcode"
                    placeholder="请输入验证码"
                    :rules="[{ required: true, message: '发送验证码' }]"
                    class="form-field"
            >
                <template #label>
                    验证码<span class="required"> *</span>
                </template>
                <template #button>
                    <van-button class="color-btn"
                            type="primary"
                            size="small"
                            round
                            :disabled="counting"
                            @click="sendVerificationCode"
                    >
                        {{ counting ? `${countDown}s后重新发送` : '获取验证码' }}
                    </van-button>
                </template>
            </van-field>


            <!-- 提交按钮 -->
            <div class="form-actions">
                <MyButton text="提交" />
            </div>
        </van-form>

        <!-- 银行选择器弹窗 -->
        <van-popup v-model:show="showBankPicker" position="bottom" round>
            <van-search
                    v-model="searchBankText"
                    placeholder="搜索银行"
                    @blur="onSearchBank"
                    @search="onSearchBank"

            />
            <van-picker
                    title="选择银行"
                    :columns="bankList"
                    @confirm="onBankConfirm"
                    @cancel="showBankPicker = false"
            />
        </van-popup>


        <!-- 提示信息 -->
        <van-toast id="van-toast"/>
    </div>
</template>

<script>
    import TopNav from "@/components/TopNav.vue";
    import MyButton from "@/components/MyButton.vue";
    import { toast } from '@/utils/toast';
    import { getBankCardList,addBank } from '@/api/index.js'
    import {useRoute} from 'vue-router';


    export default {
        components: {
            TopNav, // 注册
            MyButton
        },

        data() {
            return {
                productId:"",
                // 卡片类型
                cardType: 'debit',
                searchBankText:'',

                // 表单数据
                formData: {
                    username: '',
                    idcard: '',
                    bankName: '',
                    bankCode: '',
                    cardNumber:'',
                    phone: '',
                    vcode: ''
                },

                // 银行列表
                bankList: [

                ],

                // 弹窗控制
                showBankPicker: false,

                // 验证码倒计时
                counting: false,
                countDown: 60,

                // 正则表达式
                phoneReg: /^1[3-9]\d{9}$/,
                idCardReg: /(^\d{18}$)|(^\d{17}(\d|X|x)$)/
            };
        },
        methods: {
            // 返回上一页
            handleBack() {
                // 实际项目中使用路由返回
                // this.$router.back();
            },

            // 处理表单提交
            async handleSubmit(values) {
                console.log('表单提交:', values);
                let res = await addBank(this.formData);
                if(res.code === 200){
                    this.formData = {
                        username: '',
                        idcard: '',
                        bankName: '',
                        bankCode: '',
                        cardNumber:'',
                        phone: '',
                        code: ''
                    }
                    toast("提交成功");
                    this.$router.push(`/profile/bankCard`);
                }
            },

            // 处理表单验证失败
            handleFormError(errorInfo) {
                console.log('表单验证失败:', errorInfo);
            },

            // 取消操作
            handleCancel() {
                // 实际项目中可以重置表单或返回
                // this.$router.back();
            },


            // 选择银行确认
            onBankConfirm(value) {
                console.log('value===', value)
                this.formData.bankName = value.selectedOptions[0].text;
                this.formData.bankCode = value.selectedOptions[0].value;
                this.showBankPicker = false;
            },

            // 发送验证码
            sendVerificationCode() {

                if (!this.formData.phone) {
                    toast('请输入手机号');
                    return;
                }

                if (!this.phoneReg.test(this.formData.phone)) {
                    toast('请输入正确的手机号');
                    return;
                }

                // 开始倒计时
                this.counting = true;
                const timer = setInterval(() => {
                    this.countDown--;
                    if (this.countDown <= 0) {
                        clearInterval(timer);
                        this.counting = false;
                        this.countDown = 60;
                    }
                }, 1000);


                // 实际项目中这里会调用发送验证码的接口
            },
            async getBankCardList(){
                const res = await getBankCardList();
                if(res.code == 200){
                    this.bankList = res.data;
                }
            },
            async onSearchBank(){
                const res = await getBankCardList({keyword:this.searchBankText});
                if(res.code == 200){
                    this.bankList = res.data;
                }
            }
        },
        mounted() {
            const route = useRoute();
            let productId = route.query.productId;
            this.productId = productId;
            this.getBankCardList()
        }
    };
</script>

<style scoped>
    .payment-card-page {
        background-color: #f5f5f5;
        min-height: 100vh;
    }

    .page-header {
        background-color: #ffffff;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    }

    .payment-form {
        background-color: #ffffff;
        margin-top: 10px;
        padding: 0 16px;
    }

    .card-type-group {
        display: flex;
        padding: 16px 0;
        border-bottom: 10px solid #eee;
    }

    .card-type-item {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .card-icon {
        margin-right: 8px;
        color: #1989fa;
    }

    .form-field {
        border-bottom: 1px solid #eee;
    }

    .form-actions {
        padding: 16px;
        display: flex;
        flex-direction: column;
        gap: 10px;
    }

    .submit-btn {
        /*background: linear-gradient(90deg, #407fff, #1a60d0);*/
        background: linear-gradient(135deg, #4a90e2, #50aee5);
    }

    .cancel-btn {
        background-color: transparent;
        color: #666;
    }

    .bank-picker-popup {
        width: 100%;
    }

    .required {
        color: red;
        margin-right: 4px;
    }

    .color-btn{
        background: linear-gradient(135deg, #4a90e2, #50aee5);
    }
    /* 确保光标在移动端居中 */
    .van-search__field {
        height: 36px;
        line-height: 36px;
    }
    .van-search__input {
        height: 100%;
        line-height: 36px;
        font-size: 16px;
        padding: 0 10px;
    }


</style>
